package domain

import (
	"github.com/lab-online/internal/class/constant"
	"github.com/lab-online/internal/class/entity"
	"github.com/lab-online/internal/shared/code"
	"github.com/lab-online/pkg/auth"
)

func (d *Domain) GetClassStudent(
	classEntity entity.ClassEntity, authEntity auth.AuthEntity,
) (entity.ClassEntity, error) {
	if !classEntity.CanHandleClass(authEntity, false) {
		return nil, constant.RESTErrNoPermissionToSearchClass
	}

	classEntity, err := d.repository.GetClassWithStudentsByID(classEntity.GetID())
	if err != nil {
		return nil, code.ErrorPicker(
			err, code.DatabaseClassNotFound, constant.RESTErrClassNotFound,
		)
	}

	students, err := d.userRepository.GetManyUsersByUsername(classEntity.GetStudents())
	if err != nil {
		return nil, code.ErrorPicker(
			err, code.DatabaseUserNotFound, constant.RESTErrUserNotFound,
		)
	}

	classEntity.UpdateEntity(entity.WithStudentVOs(students))
	return classEntity, nil
}
